package com.soft.mainsite.module.wx.controller;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import com.soft.mainsite.common.domain.RestResponse;
import com.soft.mainsite.module.wx.config.WxMaConfiguration;
import com.soft.mainsite.module.wx.entity.BindInfo;
import com.soft.mainsite.module.wx.entity.WxUser;
import com.soft.mainsite.module.wx.service.WxUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author: hjm
 * @Date: 2020/12/4 23:06
 * Stay hungry,stay foolish!
 */
@Api(tags = "小程序登陆相关接口", value = "authController")
@Controller
@RequestMapping(value = "/api/wx")
public class AuthController {

    @Resource
    private WxUserService wxUserService;

    @ApiOperation(value = "小程序登陆认证接口")
    @RequestMapping(value = "/loginAuth", method = RequestMethod.GET)
    @ResponseBody
    public RestResponse loginAuth(@Valid BindInfo model) throws WxErrorException {

        String code = model.getCode();
        String appid = model.getAppid();
        String data = model.getEncryptedData();
        String iv = model.getIv();

        final WxMaService wxService = WxMaConfiguration.getMaService(appid);
        WxMaJscode2SessionResult session = wxService.getUserService().getSessionInfo(code);

        String sessionKey = session.getSessionKey();
        //获取微信用户基础信息
        String rawData = StringEscapeUtils.unescapeHtml4(data);
        WxMaUserInfo wxMaUserInfo = wxService.getUserService().getUserInfo(sessionKey, rawData, iv);

        WxUser wxUser = new WxUser();
        BeanUtils.copyProperties(wxMaUserInfo, wxUser);
        wxUser.setSessionKey(sessionKey);

        RestResponse restResponse = wxUserService.loginAuth(wxUser);

        return restResponse;
    }

    @ApiOperation(value = "绑定手机号接口")
    @GetMapping("/bindPhoneByOpenId")
    @ResponseBody
    public RestResponse bindPhoneByOpenId(String appid,
                                          String sessionKey,
                                          String signature,
                                          String rawData,
                                          String encryptedData,
                                          String iv,
                                          String openId) {

        final WxMaService wxService = WxMaConfiguration.getMaService(appid);
        // 用户信息校验
        if (!wxService.getUserService().checkUserInfo(sessionKey, rawData, signature)) {
            return RestResponse.success("", -1);
        }

        // 解密
        WxMaPhoneNumberInfo phoneNoInfo = wxService.getUserService().getPhoneNoInfo(sessionKey, encryptedData, iv);
        String phoneNumber = phoneNoInfo.getPhoneNumber();
        if (phoneNumber == null || "".equals(phoneNumber)) {
            return RestResponse.success("手机号为空", -1);
        }
        RestResponse restResponse = wxUserService.bindPhoneByOpenId(openId, phoneNumber);

        return restResponse;
    }
}
